package com.itgone.presenter.impl;

import android.app.Activity;
import android.view.View;

import com.andview.refreshview.utils.LogUtils;
import com.github.lazylibrary.util.StringUtils;
import com.github.lazylibrary.util.ToastUtils;
import com.itgone.R;
import com.itgone.bean.User;
import com.itgone.common.Const;
import com.itgone.common.ServerResponse;
import com.itgone.model.IUserModel;
import com.itgone.model.impl.UserModelImpl;
import com.itgone.presenter.IRegisterPresenter;
import com.itgone.util.AppUtil;
import com.itgone.util.JsonUtil;
import com.itgone.view.IRegisterView;
import com.zhy.http.okhttp.callback.StringCallback;

import okhttp3.Call;
import okhttp3.Request;

public class RegisterPresenterImpl implements IRegisterPresenter {

    private IRegisterView registerView;

    private IUserModel userModel = new UserModelImpl();

    public RegisterPresenterImpl(IRegisterView registerView) {
        this.registerView = registerView;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_register:
                User registerUser = registerView.getRegisterUser();
                boolean b = validateUser(registerUser);
                if (b) {
                    doRegister(registerUser);
                }
                break;
            case R.id.btn_cancel:
                AppUtil.finishActivity((Activity) registerView);
                break;
        }
    }

    private boolean validateUser(User registerUser) {
        String username = registerUser.getUsername();
        if (StringUtils.isBlank(username)) {
            registerView.setErrorMsg(Const.KEY_USERNAME, "用户名不能为空");
            return false;
        }
        String password = registerUser.getPassword();
        if (StringUtils.isBlank(password)) {
            registerView.setErrorMsg(Const.KEY_PASSWORD, "密码不能为空");
            return false;
        }
        String email = registerUser.getEmail();
        if (StringUtils.isBlank(email)) {
            registerView.setErrorMsg(Const.KEY_EMAIL, "邮箱不能为空");
            return false;
        }
        String phone = registerUser.getPhone();
        if (StringUtils.isBlank(phone)) {
            registerView.setErrorMsg(Const.KEY_PHONE, "电话不能为空");
            return false;
        }
        String question = registerUser.getQuestion();
        if (StringUtils.isBlank(question)) {
            registerView.setErrorMsg(Const.KEY_QUESTION, "问题不能为空");
            return false;
        }
        String answer = registerUser.getAnswer();
        if (StringUtils.isBlank(answer)) {
            registerView.setErrorMsg(Const.KEY_ANSWER, "答案不能为空");
            return false;
        }

        return true;
    }

    private void doRegister(User registerUser) {
        userModel.register(registerUser, new StringCallback() {

            @Override
            public void onBefore(Request request, int id) {
            }

            @Override
            public void onAfter(int id) {
            }

            @Override
            public void onError(Call call, Exception e, int id) {
                LogUtils.e("用户注册失败", e);
                ToastUtils.showToast(AppUtil.getContext(), "用户注册失败");
            }

            @Override
            public void onResponse(String response, int id) {
                ServerResponse serverResponse = JsonUtil.parseResponse(response, ServerResponse.class);
                if (serverResponse.isSuccess()) {
                    ToastUtils.showToast(AppUtil.getContext(), "注册成功");
                    AppUtil.finishActivity((Activity) registerView);
                } else {
                    ToastUtils.showToast(AppUtil.getContext(), serverResponse.getMsg());
                }
            }
        });
    }
}
